package com.forever7776.life.core.service.impl.blog;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.common.vo.*;
import com.forever7776.life.core.entity.blog.Blog;
import com.forever7776.life.core.entity.blog.BlogExt;
import com.forever7776.life.core.entity.stat.StatBlog;
import com.forever7776.life.core.mapper.blog.BlogExtMapper;
import com.forever7776.life.core.mapper.blog.BlogMapper;
import com.forever7776.life.core.mapper.stat.StatBlogMapper;
import com.forever7776.life.core.service.blog.IBlogService;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 博客主表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-02-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogExtMapper blogExtMapper;
    @Autowired
    private StatBlogMapper statBlogMapper;

    /**
     * 查询最多5条博客，首页轮播使用
     *
     * @param page
     * @return
     */
    @Override
    public Page<Blog> selectCarouselBlogs(Page<Blog> page) {
        page.setSize(5);
        return selectPage(page, new EntityWrapper<Blog>().
                eq("openup", 1).
                eq("status", 1).
                eq("type", 0).
                eq("carousel_type", 0).
                orderBy("insert_date"));
    }

    /**
     * 根据博客类型ID查询数据
     *
     * @param blogTypeId
     * @return
     */
    @Override
    public List<Blog> selectByBlogTypeId(String blogTypeId) {
        return selectList(new EntityWrapper<Blog>().like("blog_type_id", blogTypeId));
    }

    /**
     * 查询博客活跃用户排行榜
     *
     * @return
     */
    @Override
    public List<UserBaseInfoVO> selectBlogActiveUser(Integer limit) {
        return blogMapper.selectBlogActiveUser(limit);
    }

    /**
     * 根据条件查询5条博客数据
     *
     * @param type
     * @param blogTypeId
     * @return
     */
    @Override
    public List<UserBlogVO> selectUserBlogByCondition(Integer blogId, Integer type, Integer blogTypeId) {
        return blogMapper.selectUserBlogByCondition(blogId, type, blogTypeId);
    }

    /**
     * 后台管理系统查询博客数据
     *
     * @param page
     * @param vo
     * @return
     */
    @Override
    public Page<UserBlogVO> selectAllUserBlogByAdmin(Page<UserBlogVO> page, UserBlogAdminSearchVO vo) {
        List<UserBlogVO> list = blogMapper.selectAllUserBlogByAdmin(page, vo);
        page.setRecords(list);
        return page;
    }

    /**
     * 前端博客页面查询博客数据
     *
     * @param page
     * @param type
     * @param blogTypeId
     * @param userId
     * @return
     */
    @Override
    public Page<UserBlogVO> selectAllUserBlogByFront(Page<UserBlogVO> page, Integer type, Integer blogTypeId, String userId) {
        List<UserBlogVO> list = blogMapper.selectAllUserBlogByFront(page, type, blogTypeId, userId);
        page.setRecords(list);
        return page;
    }

    /**
     * 查询用户博客列表
     *
     * @param page
     * @param searchVO
     * @return
     */
    @Override
    public Page<Blog> selectAll(Page<Blog> page, UserBlogSearchVO searchVO) {
        EntityWrapper<Blog> ew = new EntityWrapper<>();
        ew.eq("user_id", searchVO.getUserId());
        if (StringUtils.isNotEmpty(searchVO.getTitle())) {
            ew.like("title", searchVO.getTitle());
        }
        if (searchVO.getType() != null) {
            ew.eq("type", searchVO.getType());
        }
        if (searchVO.getOpenup() != null) {
            ew.eq("openup", searchVO.getOpenup());
        }
        if (searchVO.getStatus() != null) {
            ew.eq("status", searchVO.getStatus());
        } else {
            ew.gt("status", "-1");
        }
        ew.orderBy("type,openup desc,last_date", false);
        return selectPage(page, ew);
    }

    /**
     * 根据用户ID和博客ID查询一条
     *
     * @param userId
     * @param id
     * @return
     */
    @Override
    public Blog selectByUserId(String userId, Integer id) {
        return selectOne(new EntityWrapper<Blog>().eq("user_id", userId).eq("id", id).gt("status", "-1"));
    }

    /**
     * 查询用户博客数量
     *
     * @return
     */
    @Cacheable
    @Override
    public Integer selectCount(String userId) {
        return blogMapper.selectCount(new EntityWrapper<Blog>().eq("user_id", userId).gt("status", "-1").eq("openup", 1));
    }

    /**
     * 保存修改博客
     *
     * @param blog
     * @param blogExt
     * @return
     */
    @Override
    public ResultVO edit(Blog blog, BlogExt blogExt) {
        BlogExt oldBlogExt = blogExtMapper.selectOneByBlogId(blog.getId());
        oldBlogExt.setContent(blogExt.getContent());
        blogMapper.updateById(blog);
        blogExtMapper.updateById(oldBlogExt);
        return ResultVoUtil.getSuccessResult("修改博客成功~");
    }

    /**
     * 创建博客
     *
     * @param blog
     * @param blogExt
     * @return
     */
    @Override
    public ResultVO save(Blog blog, BlogExt blogExt) {
        blogMapper.insert(blog);
        Integer id = blog.getId();
        blogExt.setBlogId(id);
        blogExtMapper.insert(blogExt);
        StatBlog stat = new StatBlog();
        stat.setBlogId(id);
        statBlogMapper.insert(stat);
        return ResultVoUtil.getSuccessResult("创建博客成功~");
    }

    @Override
    public ResultVO delete(Integer id) {
        blogMapper.delete(id);
        return ResultVoUtil.getSuccessResult("删除成功！");
    }

    /**
     * 修改博客审核状态
     *
     * @param blogId
     * @param type
     * @return
     */
    @Override
    public Integer updateType(Integer blogId, Integer type) {
        return blogMapper.updateType(blogId, type);
    }

    /**
     * 推荐博客
     *
     * @param blogId
     * @param recommendType
     * @return
     */
    @Override
    public Integer updateRecommendType(Integer blogId, Integer recommendType) {
        return blogMapper.updateRecommendType(blogId, recommendType);
    }

    /**
     * 修改博客是否开放状态
     *
     * @param userId
     * @param blogId
     * @param openup
     * @return
     */
    @Override
    public Integer updateOpenUp(String userId, Integer blogId, Integer openup) {
        return blogMapper.updateOpenUp(userId, blogId, openup);
    }

    /**
     * 查看博客详情，查看用户最新一条博客数据，不包括正在查看的博客
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public UserLastBlogVO selectUserLastBlog(String userId, Integer blogId) {
        return blogMapper.selectUserLastBlog(userId, blogId);
    }

    /**
     * 查看博客详情，查看5条热门博客
     *
     * @param blogId
     * @return
     */
    @Override
    public List<UserBlogBaseVO> selectFiveHotBlog(Integer blogId) {
        return blogMapper.selectFiveHotBlog(blogId);
    }

    /**
     * 根据博客审核状态查询数量
     *
     * @param type
     * @return
     */
    @Override
    public Integer selectCountByBlogType(Integer type) {
        return blogMapper.selectCountByBlogType(type);
    }
}
